﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gift.DAL;
using Gift.Model;
using System.Data;

namespace WebSite.cn
{
    public partial class Money : BasePage
    {
        readonly MemberAccount theMemberAccount = new MemberAccount();
        readonly Card theCard = new Card();
        readonly Member theMember = new Member();
        public string accountmoney = "0";
        tbMemberAccount oCardMember;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindCardList();
                this.GetAccountMoney();
            }
        }
        private void BindCardList()
        {
            DataSet ds;
            ds = theMemberAccount.GetListByMemberId(int.Parse(Session["memberid"].ToString()));
            if (ds != null && ds.Tables.Count > 0)
            {
                this.GridView1.DataSource = ds.Tables[0].DefaultView;
                this.GridView1.DataBind();
            }
        }

        private void GetAccountMoney()
        {

            if (base.UserMemberId == null)
            {
                accountmoney = "0";
            }
            else
            {
                accountmoney = theMemberAccount.GetRemainSumMoney(int.Parse(base.UserMemberId)).ToString();

            }
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(UserMemberId))
            {
                base.Response.Write("<script language=javascript>window.location.href='index.aspx';</script>");
            }
            if (string.Compare(txtValidate.Text.Trim().ToUpper(), Session["checkCode"].ToString().ToUpper()) != 0)
            {
               // this.lblMessage.Text = "验证码不正确！请重新输入！";
                Response.Write("<script>alert('验证码不正确！请重新输入！');window.location.href='Money.aspx';</script>");
                return;
            }

            dbCard oCard;
            oCard = theCard.GetModel(this.txtCardCode.Text.Trim(), this.txtCardPassword.Text.Trim());
            if (oCard != null)
            {
                if (oCard.states == 0)
                {
                    this.lblMessage.Text = "该卡还未激活，请先开卡后才能使用！";
                }
                else if (oCard.states == 2)
                {
                    this.lblMessage.Text = "该卡已被使用！";
                }
                else if (oCard.states == 3)
                {
                    this.lblMessage.Text = "该卡已失效！";
                }
                else if (oCard.states == 4)
                {
                    this.lblMessage.Text = "该卡还未激活,请先开卡后才能使用！";
                }
                else if (oCard.StartTime > DateTime.Now.Date)
                {
                    this.lblMessage.Text = "该卡还未生效！";
                }
                else if (oCard.lasttime < DateTime.Now.Date)
                {
                  
                        this.lblMessage.Text = "该卡已失效！";
                 
                }
                else if (DateTime.Now.Date > oCard.endtime && DateTime.Now.Date <= oCard.lasttime)
                {

                    if (oCard.states == 1)
                    {
                        theMemberAccount.ConvertToAccount(int.Parse(Session["memberid"].ToString()), oCard.cardid);
                        //   this.BindCardList();
                        Response.Write("<script>alert('充值成功！')</script>");
                        base.Response.Write("<script language=javascript>window.location.href='Money.aspx';</script>");
                        base.Response.End();

                    }
                    else
                    {
                        this.lblMessage.Text = "请联系客服！";
                    }


                }
                else if(DateTime.Now.Date <= oCard.endtime)
                {
                    this.lblMessage.Text = "该卡只能购买专区类商品！";
                }
            }
            else
            {
                this.lblMessage.Text = "卡号或密码不正确！";
            }
        }
    }
}